package com.botann.projects.controller;

import com.botann.projects.constants.API;
import com.botann.projects.base.BaseController;
import com.botann.projects.base.Model;
import com.botann.projects.constants.API;
import com.botann.projects.model.Dictionary;
import com.botann.projects.service.DictionaryServiceIntf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * jiyi
 * DictionaryController
 * Created by 刘山国 on 2019/9/11.
 * Copyright (c) 2017 伯坦科技. All rights reserved
 */
@RestController
public class DictionaryController extends BaseController {
    @Override
    public Class loggerClass() {
        return DictionaryController.class;
    }


    @Autowired
    private DictionaryServiceIntf service;


    @RequestMapping(value = API.URL_DICTIONARIES, method = RequestMethod.POST)
    public Model loadDics(Integer pageIndex, Integer pageSize, String mkey, String name){
        try {
            return successModel(service.loadDictionaries(pageIndex,pageSize,mkey,name));
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = API.URL_DICTIONARY_ADD_UPDATE, method = RequestMethod.POST)
    public Model updateDic(@RequestParam String json){
        try {
            service.saveOrUpdateDic(json);
            return successModel();
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = API.URL_DICTIONARY_DELETE, method = RequestMethod.POST)
    public Model deleteDic(@RequestParam String deleteIdsJson){
        try {
            service.deleteDics(deleteIdsJson);
            return successModel();
        } catch (Exception e){
            return failureModel(e);
        }
    }



    @RequestMapping(value = "/public/dics", method = RequestMethod.POST)
    public Model loadDics(@RequestParam String mkey, String name){
        try {
            return successModel(service.loadDictionaries(mkey,name));
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = "/public/dic", method = RequestMethod.POST)
    public Model loadDic(@RequestParam String mkey, String name){
        try {
            List<Dictionary> dics = service.loadDictionaries(mkey,name);
            Map<String,String > map = new HashMap<>();
            for (Dictionary dic : dics) {
                map.put(dic.getName(),dic.getValue());
            }
            return successModel(map);
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = "/public/dicValue", method = RequestMethod.POST)
    public Model loadDicValue(@RequestParam String mkey, String name){
        try {
            List<Dictionary> dics = service.loadDictionaries(mkey,name);
            if (dics.size()>0) {
                return successModel(dics.get(0).getValue());
            } else {
                return failureModel("没有该数据!");
            }
        } catch (Exception e){
            return failureModel(e);
        }
    }

    @RequestMapping(value = "/public/dicValues", method = RequestMethod.POST)
    public Model loadDicValues(@RequestParam String mkey, String name){
        try {
            List<Dictionary> dics = service.loadDictionaries(mkey,name);
            ArrayList list = new ArrayList();
            for (Dictionary dic : dics) {
                list.add(dic.getValue());
            }
            return successModel(list);
        } catch (Exception e){
            return failureModel(e);
        }
    }
}
